package controllers

import (
	"MaternalBaby/api/models"

	"github.com/oldfeel/ofutils"

	"github.com/dchest/captcha"
)

type User struct {
	BaseController
}

// @router /vcodeId [get]
func (c *User) VCodeId() {
	cid := captcha.New()
	c.Success(cid)
}

// @router /vcodeImage [get]
func (c *User) VCodeImage() {
	cid := c.GetString("cid")

	err := captcha.WriteImage(c.Ctx.ResponseWriter, cid, 100, 40)
	if err != nil {
		c.Ctx.WriteString("验证码Id已过期,请点击验证码图片重新获取")
	}
}

// @router /checkPhone [get]
func (c *User) CheckPhone() {
	phone := c.GetString("phone")

	var u models.User
	exist := o.QueryTable(u).Filter("Phone", phone).Exist()

	if exist {
		c.Fail(1, "手机号码已存在")
		return
	}
	c.Success("")
}

// @router /checkEmail [get]
func (c *User) CheckEmail() {
	email := c.GetString("email")

	var u models.User
	exist := o.QueryTable(u).Filter("Email", email).Exist()

	if exist {
		c.Fail(1, "邮箱已存在")
		return
	}
	c.Success("")
}

// @router /register [get]
func (c *User) Register() {
	phone := c.GetString("phone")
	email := c.GetString("email")
	password := c.GetString("password")
	vcode := c.GetString("vcode")
	vcodeId := c.GetString("vcodeId")
	deviceId := c.GetString("deviceId")

	if !captcha.VerifyString(vcodeId, vcode) {
		c.Fail(4, "验证码错误")
		return
	}

	var u models.User

	exist := o.QueryTable(u).Filter("Phone", phone).Exist()
	if exist {
		c.Fail(1, "手机号码已存在")
		return
	}

	exist = o.QueryTable(u).Filter("Email", email).Exist()
	if exist {
		c.Fail(2, "邮箱已存在")
		return
	}

	if len(password) < 6 || len(password) > 16 {
		c.Fail(3, "密码必需 6-16 位")
		return
	}

	u = models.User{
		Phone:    phone,
		Email:    email,
		Password: ofutils.MD5(password),
		Token:    ofutils.RandString(32),
		DeviceId: deviceId,
	}
	_, err := o.Insert(&u)
	if err != nil {
		c.Fail(5, err)
		return
	}
	c.Success(u.Info())
}

// @router /login [get]
func (c *User) Login() {
	account := c.GetString("account")
	password := c.GetString("password")
	deviceId := c.GetString("deviceId")

	var u models.User

	err := o.Raw("select * from user where (phone=? or email=?) and password=?", account, account, ofutils.MD5(password)).QueryRow(&u)
	if err != nil {
		c.Fail(1, "账号或密码错误")
		return
	}

	u.Token = ofutils.RandString(32)
	u.DeviceId = deviceId
	o.Update(&u, "Token", "DeviceId")

	c.Success(u.Info())
}

// @router /tryOut [get]
func (c *User) TryOut() {
	deviceId := c.GetString("deviceId")

	u := models.User{DeviceId: deviceId}
	err := o.QueryTable(u).Filter("DeviceId", deviceId).One(&u)
	if err == nil {
		u.Token = ofutils.RandString(32)
		o.Update(&u, "Token")

		c.Success(u.Info())
		return
	}

	u.Token = ofutils.RandString(32)
	_, err = o.Insert(&u)
	if err != nil {
		c.Fail(1, err)
		return
	}
	c.Success(u.Info())
}

// @router /info [get]
func (c *User) Info() {
	token := c.GetString("Token")
	if !c.isLogin && token != "" {
		c.Fail(1, "登录已过期,请重新登录")
		return
	}

	c.Success(c.u.Info())
}

// @router /updatePhone [get]
func (c *User) UpdatePhone() {
	phone := c.GetString("phone")

	exist := o.QueryTable(c.u).Filter("Phone", phone).Exist()
	if exist {
		c.Fail(1, "手机号码已存在")
		return
	}

	c.u.Phone = phone

	o.Update(&c.u, "Phone")
	c.Success(c.u.Info())
}

// @router /updateName [get]
func (c *User) UpdateName() {
	name := c.GetString("name")

	c.u.Name = name
	o.Update(&c.u, "Name")
	c.Success(c.u.Info())
}

// @router /updateEmail [get]
func (c *User) UpdateEmail() {
	email := c.GetString("email")

	exist := o.QueryTable(c.u).Filter("Email", email).Exist()
	if exist {
		c.Fail(1, "邮箱已存在")
		return
	}

	c.u.Email = email

	o.Update(&c.u, "Email")
	c.Success(c.u.Info())
}

// @router /updateAvatar [get]
func (c *User) UpdateAvatar() {
	avatar := c.GetString("avatar")
	c.u.Avatar = avatar

	_, err := o.Update(&c.u, "Avatar")
	if err != nil {
		c.Fail(1, err)
		return
	}
	c.Success(c.u.Info())
}
